package com.example.demo.mapper;

import com.example.demo.bean.ChildPoi;
import com.example.demo.bean.Poi;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;
@Mapper
public interface PoiMapper {
    @Select("SELECT produce_goods_type FROM poi WHERE poi_id = #{poi_id}")
    Map<String, Object> getProduce_goods_type(@Param("poi_id") int poi_id);

    @Select("select need_goods_type from poi where poi_id = #{poi_id}")
    int selectGoods(@Param("poi_id") long destination);

    @Select("select poi_id from poi where produce_goods_type = #{produce_goods_type}")
    List<Map<String, Object>> getOwners(@Param("produce_goods_type") int need_goods_type);

    @Select("select integration_distance from poi where poi_id = #{poi_id}")
    double getIntegrations(@Param("poi_id") int destination);

    @Select("select poi_lng,poi_lat from poi where poi_id = #{poi_id}")
    Map<String, Object> getPoi_position(@Param("poi_id") int poi_id);

    @Update("update poi set out_stock = LEAST(out_stock + consume_number,max_stock) where tache = 1")
    void produce();

    @Update("update poi " +
            "set out_stock = LEAST(LEAST(in_stock,consume_number) * transformation_rate + out_stock,max_stock), " +
            "in_stock = GREATEST(in_stock - consume_number,0) " +
            "where tache = 2 or tache = 3 or tache = 4")
    void translate();

    @Update("update poi set out_stock = GREATEST(0,out_stock-consume_number * 0.3) where tache = 4")
    void sale();

    @Select("select poi_id,industry_id,max_stock,transformation_rate " +
            "from poi " +
            "where tache = 4 and out_stock < 0.125 * max_stock")
    List<Poi> search_last_factory();

    @Select("select poi_id,integration_distance,out_stock,transformation_rate,industry_id from poi where industry_id = #{industry_id} and tache = #{tache}")
    List<Poi> search_tache_factory(@Param("tache") int tache,@Param("industry_id") int industry_id);

    @Update("update poi set out_stock = out_stock + #{goods_in} * transformation_rate - #{goods_out} where poi_id = #{poi_id}")
    void change_out_stock(@Param("goods_in") double goods_in,@Param("goods_out") double goods_out,@Param("poi_id") int poi_id);

    @Update("update poi set out_stock = out_stock - #{goods_out} where poi_id = #{poi_id}")
    void change_out_stock0(@Param("goods_out") double goods_out,@Param("poi_id") int poi_id);

    @Update("update poi set in_stock = in_stock + #{goods_in} where poi_id = #{poi_id}")
    void change_in_stock(@Param("goods_in") double goods_in,@Param("poi_id") int poi_id);

    @Select("select count(poi_id)"+
            "from poi join distance " +
            "on distance.end_poi_id = poi.poi_id " +
            "where distance.start_poi_id = #{poi_id} " +
            "and poi.industry_id = #{industry_id} " +
            "and poi.tache = #{tache} " )
    int search_next_tache_factory_num(@Param("tache") int tache, @Param("industry_id") int industry_id,@Param("poi_id") int previous_id);

    @Select("select poi.poi_id,poi.industry_id,poi.integration_distance,poi.out_stock,poi.transformation_rate,distance.Distance,distance.Duration " +
            "from poi join distance " +
            "on poi.poi_id = distance.end_poi_id "+
            "where distance.start_poi_id = #{poi_id} " +
            "and poi.industry_id = #{industry_id} " +
            "and poi.tache = #{tache} " +
            "order by ((2-LEAST(1,poi.out_stock / #{need_remain})) * distance.Duration)"+
            "LIMIT #{nums}")
    List<ChildPoi> search_next_tache_factory(@Param("tache") int tache, @Param("industry_id") int industry_id,@Param("poi_id") int previous_id,@Param("need_remain") double need_remain,@Param("nums") int nums);
}
